package com.homegoods.designer.util;

import android.text.TextPaint;
import android.view.View;
import android.widget.TextView;

/**
 * Created by slj on 2015/3/3.
 */
public class ShowIntroHelper implements View.OnClickListener {
    private final static int MAX_NUM = 4;
    private TextView mDisplay;
    private TextView mMore;
    private boolean isShowMore = false;

    public ShowIntroHelper(TextView display, TextView mMore) {
        this.mDisplay = display;
        this.mMore = mMore;
        this.mMore.setOnClickListener(this);
    }

    public void setIntro(CharSequence intro) {
        if (mDisplay == null) {
            return;
        }
        mDisplay.setText(intro);
        TextPaint paint = mDisplay.getPaint();
        final int len = (int) paint.measureText(intro.toString());
        mDisplay.post(() -> {
            int num = len / mDisplay.getWidth() + 1;
            if (num <= MAX_NUM) {
                mDisplay.setMaxLines(num < 1 ? 1 : num);
                mMore.setVisibility(View.GONE);
            } else {
                mDisplay.setMaxLines(MAX_NUM);
                mMore.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (isShowMore) {
            //收起
            mDisplay.setMaxLines(MAX_NUM);
            mMore.setText("查看更多");
        } else {
            //展开
            mDisplay.setMaxLines(Integer.MAX_VALUE);
            mMore.setText("收起");
        }
        isShowMore = !isShowMore;
    }
}
